package com.jacksay.vikingfl.combobox
{
	import com.jacksay.vikingfl.core.IBusinessComponent;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.ComboBox;
	import mx.events.ListEvent;

	public class VkComboBox extends ComboBox implements IBusinessComponent
	{
		public function VkComboBox()
		{
			super();
			addEventListener(ListEvent.CHANGE, handlerChange);
				
		}
		
		
		
		// ---------------------------------------------------------------------
		//
		// FLAGS
		//
		// ---------------------------------------------------------------------
		private var _fselectedPrimary:Boolean = false;
		private var _fprimaryName:Boolean = false;
		private var _fdataProvider:Boolean = false;
		
		
		
		// ---------------------------------------------------------------------
		//
		// PROPERTIES
		//
		// ---------------------------------------------------------------------
		
		// ---------------------------------------------------------------------
		// selectedPrimary
		// ---------------------------------------------------------------------
		/**
		 * Valeur de l'dentifiant de l'objet selectionné.
		 * 
		 * @default	-1
		 * @author	jacksay14
		 * @since	2010-03-24
		 */
		[Bindable("selectedPrimaryChange")]
		[Inspectable(type='int', defaultValue='-1')]
		public function get selectedPrimary() :int {
			return _selectedPrimary;
		}
		
		/**
		 * @private
		 */
		private var _selectedPrimary:int = -1;
		
		/**
		 * @private
		 */
		public function set selectedPrimary( value:int ):void{
			if( value != _selectedPrimary ){
				trace("Affectation de " +value);
				_fselectedPrimary = true;
				_selectedPrimary = value;
				invalidateProperties();
			}
		}
		
		
		
		// ---------------------------------------------------------------------
		// primaryName
		// ---------------------------------------------------------------------
		/**
		 * Nom de la propriété utilisée comme identifiant unique de les objets.
		 * 
		 * @default	"id"
		 * @author	jacksay14
		 * @since	2010-03-24
		 */
		[Inspectable(type='String', defaultValue='id')]
		public function get primaryName() :String{
			return _primaryName;
		}
		
		/**
		 * @private
		 */
		private var _primaryName:String = "id";
		
		/**
		 * @private
		 */
		public function set primaryName( value:String ) :void {
			if( value != _primaryName ){
				_fprimaryName = true;
				_primaryName = value;
				invalidateProperties();
			}
		}
		
		
		
		// ---------------------------------------------------------------------
		// defaultData
		// ---------------------------------------------------------------------
		public function get defaultData() :* {
			return _defaultData;
		}
		
		private var _defaultData:* = null;
		
		public function set defaultData( value:* ) :void {
			_defaultData = value;
		}
		
		
		
		// ---------------------------------------------------------------------
		//
		// PROPERTIES : OVERRIDE
		//
		// ---------------------------------------------------------------------
		
		// ---------------------------------------------------------------------
		// dataProvider
		// ---------------------------------------------------------------------
		override public function set dataProvider( value:Object ) :void {
			if( value != super.dataProvider ){
				_fdataProvider = true;
				super.dataProvider = value;
				invalidateProperties();
			}
		}
		
		
		
		// ---------------------------------------------------------------------
		// selectedIndex
		// ---------------------------------------------------------------------
		override public function set selectedIndex( value:int ) :void {
			if( super.selectedIndex != value ){
				if( value < 0 ) selectedPrimary = -1;
				else selectedPrimary = ArrayCollection(dataProvider).getItemAt(value)[primaryName];
			}
		}
		
		
		
		// ---------------------------------------------------------------------
		// selectedItem
		// ---------------------------------------------------------------------
		override public function set selectedItem( value:Object ) :void {
			if( super.selectedItem != value ){
				if( value == null ) selectedPrimary = -1;
				else selectedPrimary = value[primaryName];
			}
		}
		
		
		
		
		
		
		
		
		// ---------------------------------------------------------------------
		//
		// METHODES : HANDLER
		//
		// ---------------------------------------------------------------------
		
		/** Executée lors qu'une interaction utilisateur modifie la donnée
		 * selectionnée.
		 * 
		 * @author	jacksay14
		 * @since	2010-03-25
		 */
		protected function handlerChange( e:ListEvent ) :void {
			if( selectedItem != null ){
				selectedPrimary = selectedItem[primaryName];
			} else {
				selectedPrimary = -1;
			}
		}
		
		
		
		// ---------------------------------------------------------------------
		//
		// METHODES : OVERRIDE
		//
		// ---------------------------------------------------------------------
		
		
		override protected function commitProperties() : void {
			trace(this.className+".commitProperties()");
			super.commitProperties();
			
			if( _fselectedPrimary ){
				_fselectedPrimary = false;
				super.selectedItem = getItemByPrimaryValue(selectedPrimary);
				dispatchEvent(new Event("selectedPrimaryChange"));
			}
		}
		
		public function getItemByPrimaryValue( value:int ) :* {
			var item:Object;
			
			for each( item in dataProvider ){
				if (item.hasOwnProperty(primaryName) && item[primaryName] == value) {
					return item;
				}
			} 
			return null;
		}
	}
}